// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   PrivilegeServiceImp.java

package com.iphaeton.lms.business.role;

import com.iphaeton.common.helper.Pagination;
import com.iphaeton.exception.BusinessException;
import com.iphaeton.exception.PersistenceException;
import com.iphaeton.lms.business.PersistentServiceSupport;
import com.iphaeton.lms.pojo.role.Privilege;
import com.iphaeton.persistence.IPersistenceService;
import com.iphaeton.persistence.spring.JdbcPService;
import java.util.*;
import org.apache.log4j.Logger;

// Referenced classes of package com.iphaeton.lms.business.role:
//            IPrivilegeService

public class PrivilegeServiceImp extends PersistentServiceSupport
    implements IPrivilegeService
{

    public PrivilegeServiceImp()
    {
    }

    public Privilege save(Privilege p)
        throws BusinessException
    {
        if(p == null)
            throw new BusinessException("null Privilege isn't allowed in function save(Privilege p)!");
        Privilege privilege = null;
        try
        {
            privilege = (Privilege)hbpservice.store(p);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("can't save Privilege  '" + p.getName() + "'!");
        }
        return privilege;
    }

    public Privilege findPrivilegeById(String id)
        throws BusinessException
    {
        if(id == null)
            throw new BusinessException("null id isn't allowed in function findPrivilegeById(String id)!");
        Privilege p = null;
        try
        {
            p = (Privilege)hbpservice.get(id, com.iphaeton.lms.pojo.role.Privilege.class);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("can't find Privilege by id:" + id + "!");
        }
        return p;
    }

    public List findPageList(Pagination pageInfo)
        throws BusinessException
    {
        return getSpecifiedItems(pageInfo);
    }

    public Pagination findPrivilegePagination(String roleId, int pageSize)
        throws BusinessException
    {
        String selectHql = "select count(p) from Privilege p where p.role.id='" + roleId + "'";
        String hqlQueryStr = "from Privilege p where p.role.id='" + roleId + "' order by p.name";
        List list = null;
        try
        {
            list = hbpservice.query(selectHql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("can't find any privilege from system!");
        }
        int itemCount = list.size() <= 0 ? 0 : ((Integer)list.get(0)).intValue();
        int totalPage = ((itemCount + pageSize) - 1) / pageSize;
        Pagination pageInfo = new Pagination(totalPage, itemCount, pageSize);
        pageInfo.setHqlQueryString(hqlQueryStr);
        return pageInfo;
    }

    public Pagination findAllPrivilegePagination(String row, int pageSize)
        throws BusinessException
    {
        String selectHql = "select count(p) from Privilege p";
        String hqlQueryStr = "from Privilege p";
        if("".equals(row))
            hqlQueryStr = hqlQueryStr + " order by p.name";
        else
            hqlQueryStr = hqlQueryStr + " order by p." + row;
        List list = null;
        try
        {
            list = hbpservice.query(selectHql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("can't find any privilege from system!");
        }
        int itemCount = list.size() <= 0 ? 0 : ((Integer)list.get(0)).intValue();
        int totalPage = ((itemCount + pageSize) - 1) / pageSize;
        Pagination pageInfo = new Pagination(totalPage, itemCount, pageSize);
        pageInfo.setHqlQueryString(hqlQueryStr);
        return pageInfo;
    }

    public List findPrivilege(int sort)
        throws BusinessException
    {
        String hql = "from Privilege p";
        if(sort != 0)
            hql = hql + " where p.sort='" + sort + "'";
        List list = null;
        try
        {
            list = hbpservice.query(hql);
        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("can't find any privilege by sort:" + sort);
        }
        return list;
    }

    public List findNotRolePrivilege(String id)
        throws BusinessException
    {
        String sql = "select p.id as id, p.name as name, p.comment as comment from t_privilege p where p.id not in (select rp.privilege_id from t_role_privilege rp where rp.role_id = '" + id + "')";
        List list = new LinkedList();
        try
        {
            List temp = jdbcpservice.query(sql);
            for(int i = 0; i < temp.size(); i++)
            {
                Privilege p = new Privilege();
                p.setId((String)((Map)temp.get(i)).get("id"));
                p.setName((String)((Map)temp.get(i)).get("name"));
                p.setComment(((Map)temp.get(i)).get("comment") == null ? null : (String)((Map)temp.get(i)).get("comment"));
                list.add(p);
            }

        }
        catch(PersistenceException e)
        {
            e.printStackTrace();
            throw new BusinessException("\u67E5\u8BE2\u5F53\u524D\u89D2\u8272\u4E2D\u6CA1\u6709\u7684\u6743\u9650\u65F6\u5931\u8D25!");
        }
        return list;
    }

    private static Logger logger;
    static Class class$0; /* synthetic field */
    static Class class$1; /* synthetic field */

    static 
    {
        logger = Logger.getLogger(com.iphaeton.lms.business.role.PrivilegeServiceImp.class.getName());
    }
}
